<template>
  <div class="class-card">
    <article>我的课程</article>
    <el-carousel :interval="5000" trigger="click" @change="onChange">
      <el-carousel-item v-for="orderNum in carouselItemNum" :key="orderNum">
        <div class="classes-card-item" v-for="item in showArray">
          <h3>{{ item.teachClass }}</h3>
          <p>
            年级：<span>{{ item.grade }}级</span>
          </p>
          <p>
            学年：<span>{{ item.schoolYear }}学年</span>
          </p>
          <p>
            学期：<span>第{{ item.stage }}学期</span>
          </p>
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useUserStore } from '../../../../store/user';
import { APIData, ITchCoYear, ITchCoYearParams } from '../../../../type';
import { ElMessage } from 'element-plus';
import { useAPI } from '../../../../request/api';
import { onMounted } from 'vue';

onMounted(async () => {
  await getData();
  carouselItemNum.value = originArray.length % cradNum == 0 ? originArray.length / cradNum : Math.floor(originArray.length / cradNum) + 1;
  showArray.value = originArray.slice(startIndex.value, endIndex.value);
})

const cradNum = 4;
const api = useAPI();
const userStore = useUserStore();
const startIndex = ref<number>(0);
const endIndex = ref<number>(cradNum);
let originArray = [] as ITchCoYear[];
const carouselItemNum = ref(0);
const showArray = ref([] as ITchCoYear[]);


const onChange = (currentIndex: number) => {
  startIndex.value = currentIndex * cradNum;
  endIndex.value = startIndex.value + cradNum;
  if (endIndex.value > originArray.length) endIndex.value = originArray.length;
  showArray.value = originArray.slice(startIndex.value, endIndex.value);
};

const getData = async () => {
  const params: ITchCoYearParams = { workId: userStore.userInfo.workId }
  const res: APIData = await api.course.findTchCoYear(params);
  if (res.code === 200) originArray = res.data;
  else ElMessage.error(res.message);
};

</script>

<style lang="scss" scoped>
.class-card {
  height: 100%;
  // position: relative;
  article {
    // position: absolute;
    // height: 30%;
    padding: 16px;
    padding-bottom: 0;
    font-size: 24px;
    letter-spacing: 2px;
  }
}
.el-carousel {
  height: 80%;
}
:deep(.el-carousel__container) {
  height: 100%;
}
.classes-card-item {
  color: #e7e9ec;
  // color: #000;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin: 8px 16px 0 16px;
  padding: 12px;
  // width: 220px;
  width: 20%;
  height: 126px;
  border-radius: 16px;
  transition: transform 0.3s;
  box-shadow: 0 4px 12px 4px rgba(0, 0, 0, 0.2);

  h3 {
    font-size: 18px;
    letter-spacing: 2px;
  }
  span {
    font-weight: bold;
  }
}
.classes-card-item:hover {
  cursor: pointer;
  transition: transform 0.3s;
  transform: scale(1.2);
}

.classes-card-item:nth-child(1) {
  background-image: linear-gradient(135deg, #b391fc, #7b75fc);
}

.classes-card-item:nth-child(2) {
  background-image: linear-gradient(135deg, #ff9d98, #ffc19f);
}

.classes-card-item:nth-child(3) {
  background-image: linear-gradient(135deg, #f5bdc9, #aa85d7);
}

.classes-card-item:nth-child(4) {
  background-image: linear-gradient(135deg, #00e6d1, #25c5f4);
}

.el-carousel__item {
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-carousel__button) {
  height: 3px;
  background-color: var(--font-color);
}
:deep(.el-carousel__indicator.is-active button) {
  height: 3px;
  background-color: var(--active-color);
}
// .el-carousel__item:nth-child(2n) {
//   border-radius: 16px;
//   background-color: #99a9bf;
// }

// .el-carousel__item:nth-child(2n + 1) {
//   border-radius: 16px;
//   background-color: #d3dce6;
// }
</style>
